<?php
namespace Home\Controller;
use Common\Controller\CommonController;

/**
 * 
 * @author 张亚伟 <zhangyw@zhijin101.com>
 */
class JpTalkController extends CommonController
{
    public $model;
    
    public function _initialize(){ 
        parent::_initialize();
        $this->model = M('JpTalk');
    }
    public function index(){
        $map = [];
        list($list, $page) = $this->lists($this->model, $map, 'sort desc,id asc');
        $this->assign('page', $page);
        $this->assign('list', $list);
        $this->display();
    }

    public function add(){
        if(IS_POST){
            $data = I('post.');
            if(empty($data['title'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入标题'));
            }
            if(empty($data['contents'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请填写话术内容'));
            }
            foreach ($data['contents'] as $key => $content) {
                if(empty($content['title'])){
                    $this->ajaxReturn(array('s'=>false,'m'=>'话术内容请填写完整'));
                }
                if(empty($content['wds'])){
                    $this->ajaxReturn(array('s'=>false,'m'=>'话术内容请填写完整'));
                }
                foreach ($content['wds'] as $key => $wd) {
                    if(empty($wd['text']) || empty($wd['type'])){
                        $this->ajaxReturn(array('s'=>false,'m'=>'话术内容请填写完整'));
                    }
                }
            }
            $data['contents'] = json_encode($data['contents']);
            $data['create_time'] = time();
            $data['update_time'] = time();
            $id = $this->model->add($data);
            if($id){
                $this->ajaxReturn(array('s'=>true,'m'=>'添加成功', 'url'=>U('index')));
            }else{
                $this->ajaxReturn(array('s'=>false,'m'=>'添加失败!'));
            }
        }else{
            $sort = $this->model->max('sort')+1;
            $this->assign('sort', $sort);
            $this->display();
        }
    }

    public function edit(){
        $id = I('id');
        if (IS_POST) {
            $data = I('post.');
            if(empty($data['title'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入标题'));
            }
            if(empty($data['contents'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请填写话术内容'));
            }
            foreach ($data['contents'] as $key => $content) {
                if(empty($content['title'])){
                    $this->ajaxReturn(array('s'=>false,'m'=>'话术内容请填写完整'));
                }
                if(empty($content['wds'])){
                    $this->ajaxReturn(array('s'=>false,'m'=>'话术内容请填写完整'));
                }
                foreach ($content['wds'] as $key => $wd) {
                    if(empty($wd['text']) || empty($wd['type'])){
                        $this->ajaxReturn(array('s'=>false,'m'=>'话术内容请填写完整'));
                    }
                }
            }
            $data['contents'] = json_encode($data['contents']);
            $data['update_time'] = time();
            if($this->model->create($data)){
                $res = $this->model->where(['id'=>$id])->save();
                if($res !== false){
                    $this->ajaxReturn(['s'=>true, 'm'=>'修改成功', 'url' => U('index')]);
                }else{
                    $this->ajaxReturn(['s'=>false, 'm'=>'修改失败']);
                }
            }else{
                $this->ajaxReturn(['s'=>false, 'm'=>"#修改失败"]);
            }
        } else {
            $info = $this->model->where(['id' => $id])->find();
            if(empty($info)){
                $this->error('信息不存在');
            }
            $info['contents'] = json_decode($info['contents'], true);
            $info['contents'] = $info['contents'] ? $info['contents'] : [];
            $this->assign('info', $info);
            $this->display();
        }
    }

    public function del(){
        $id = I('id');
        $res = $this->model->where(['id' => $id])->delete();
        if ($res) {
            $this->ajaxReturn(array('s' => true, 'm' => '删除成功', 'url' => U('index')));
        } else {
            $this->ajaxReturn(array('s' => false, 'm' => '删除失败!'));
        }
    }
}